﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThanNhanTao.Server.BLL;
using ThanNhanTao.Server.Entities;
using ThanNhanTao.CommonControl;
using DevExpress.Xpf.Core;
using DevExpress.Xpf.Grid;

namespace ThanNhanTao.UC
{
    /// <summary>
    /// Interaction logic for ucDanhSachHoSoNhanSu.xaml
    /// </summary>
    public partial class ucDanhSachHoSoNhanSu : UserControl
    {
        public DXWindow formContain { get; set; }

        HoSoNhanSuBLL hoSobll;
        public ucDanhSachHoSoNhanSu()
        {
            InitializeComponent();
            hoSobll = new HoSoNhanSuBLL();
        }

        public void BindHoSuNhanSu()
        {
            if (ThanNhanTaoGlobal.ListHoSoNhanSu.Count == 0)
                ThanNhanTaoGlobal.ListHoSoNhanSu = hoSobll.RetrieveAllHoSoNhanSu();
            gridNhanSu.ItemsSource = ThanNhanTaoGlobal.ListHoSoNhanSu;
        }

        public void AddHoSoNhanSuToGrid(HoSoNhanSuEntities hoSo)
        {
            try
            {
                ThanNhanTaoGlobal.ListHoSoNhanSu.Add(hoSo);
                gridNhanSu.ItemsSource = ThanNhanTaoGlobal.ListHoSoNhanSu.OrderBy(u => u.Ten);
                gridNhanSu.RefreshData();
                tbvNhanSu.FocusedRow = hoSo;
            }
            catch (Exception)
            {
                DXMessageBox.Show("Thêm thông tin vào danh sách không thành công", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        public void UpdateHoSoNhanSuToGrid(HoSoNhanSuEntities hoSo)
        {
            try
            {
                HoSoNhanSuEntities hoSoDangDung = ThanNhanTaoGlobal.ListHoSoNhanSu.FirstOrDefault(u => u.IdHoSoNhanSu == hoSo.IdHoSoNhanSu);
                if (hoSoDangDung != null)
                {
                    hoSoDangDung = hoSo;
                    gridNhanSu.RefreshData();
                    tbvNhanSu.FocusedRow = hoSo;
                }
            }
            catch (Exception)
            {
                DXMessageBox.Show("Cập nhật thông tin vào danh sách không thành công", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        public void DeleteHoSoNhanSuToGrid(int idHoSoNhanSu)
        {
            try
            {
                ThanNhanTaoGlobal.ListHoSoNhanSu.RemoveAll(u => u.IdHoSoNhanSu == idHoSoNhanSu);
                gridNhanSu.ItemsSource = ThanNhanTaoGlobal.ListHoSoNhanSu;
                gridNhanSu.RefreshData();
            }
            catch (Exception)
            {
                DXMessageBox.Show("Xóa thông tin trong danh sách không thành công", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void tbvNhanSu_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (gridNhanSu.View.GetRowHandleByMouseEventArgs(e) == GridControl.InvalidRowHandle)
                return;
            if (this.gridNhanSu.GetFocusedRow() == null)
                return;
            HoSoNhanSuEntities hoSoHienTai= gridNhanSu.GetFocusedRow() as HoSoNhanSuEntities;
            if (formContain is frmPhieuThongTinHoSoCongChucVienChuc)
            {
                frmPhieuThongTinHoSoCongChucVienChuc formMain = formContain as frmPhieuThongTinHoSoCongChucVienChuc;
                formMain.BindHoSoNhanSuHienTai(hoSoHienTai);
            }
        }

        private void tbvNhanSu_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (this.gridNhanSu.GetFocusedRow() == null)
                return;
            HoSoNhanSuEntities hoSoHienTai = gridNhanSu.GetFocusedRow() as HoSoNhanSuEntities;
            if (formContain is frmPhieuThongTinHoSoCongChucVienChuc)
            {
                frmPhieuThongTinHoSoCongChucVienChuc formMain = formContain as frmPhieuThongTinHoSoCongChucVienChuc;
                formMain.btnXoa.IsEnabled = true;
            }
        }
    }
}
